package com.yibo.user.service.user;

import com.yibo.db.user.entity.LoginError;
import com.yibo.db.user.entity.User;
import com.yibo.db.user.entity.UserInfo;
import com.yibo.db.user.entity.UserLevel;
import com.yibo.db.user.mapper.loginerror.LoginErrorMapper;
import com.yibo.db.user.mapper.user.UserMapper;
import com.yibo.db.user.mapper.userinfo.UserInfoMapper;
import com.yibo.db.user.mapper.userlev.UserLevelMapper;
import com.yibo.feign.message.CheckWordService;
import com.yibo.support.exception.YBException;
import com.yibo.support.helper.Assert;
import com.yibo.support.helper.ObjectHelper;
import com.yibo.support.helper.TimeHelper;
import com.yibo.support.pojo.IResult;
import com.yibo.user.object.YBUser;

import java.util.Collections;
import java.util.List;

import static com.yibo.support.constant.MsgConstant.*;
import static com.yibo.user.constant.Constant.EMAIL;

/**
 * 通过邮箱验证码登陆命令
 *
 * @author zhanglei
 */
public class LoginByEmailCheckWordCommand extends BaseLoginCommand {

    /**
     * 邮箱
     */
    private String email;

    /**
     * 邮箱验证码
     */
    private String checkWord;


    /**
     * 登陆错误信息
     */
    private LoginError loginError;

    public LoginByEmailCheckWordCommand(String email,String checkWord){
        this.email = email;
        this.checkWord = checkWord;
    }

    @Override
    public YBUser login() {
        initIp();

        //校验参数
        checkArgs();

        //用户信息
        UserInfo info = infoByEmail();

        //登陆
        login(info);

        return this.result;
    }


    private UserInfo infoByEmail(){
        LoginErrorMapper errorMapper = getBean(LoginErrorMapper.class);
        UserInfoMapper mapper = getBean(UserInfoMapper.class);
        List<UserInfo> infos = mapper.selectByMap(Collections.singletonMap(EMAIL,this.email));
        if(ObjectHelper.isEmpty(infos)){
            saveLoginError(errorMapper,this.loginError,null);
            throw new YBException(EMAIL_LOGIN_ERROR);
        }

        //邮箱验证码
        CheckWordService checkWordServer = getBean(CheckWordService.class);
        IResult<String> result = checkWordServer.emailWord(this.email, "login");

        String word = result.getData();
        if(!this.checkWord.equals(word)){
            saveLoginError(errorMapper,this.loginError,null);
            throw new YBException(EMAIL_CHECK_WORD_ERROR);
        }
        return  infos.get(0);
    }


    private void  login(UserInfo info){
        //校验redis信息
        this.result = checkRedisMsg(info.getUserId());
        if(result != null){
            return;
        }
        result = new YBUser();

        //用户基本信息
        User user = getBean(UserMapper.class).selectById(info.getUserId());

        UserLevelMapper levelMapper = getBean(UserLevelMapper.class);
        UserLevel level = levelMapper.selectById(info.getUserId());
        Assert.notEmpty(USER_MESSAGE_QUERY_FAIL,level);
        this.result.setUserId(info.getUserId())
                .setUserName(user.getUserName())
                .setExtInfo(info)
                .setUserLevel(level)
                .setIp(this.ip)
                .setLastQueryTime(TimeHelper.getNow())
                .setLastLoginTime(TimeHelper.getNow());

        //添加用户信息到redis缓存
        addRedis(info.getUserId());
        //删除错误登陆信息
        getBean(LoginErrorMapper.class).deleteById(this.ip);
    }



    private void checkArgs(){
        Assert.notEmpty(EMAIL_IS_EMPTY,this.email);
        Assert.notEmpty(EMAIL_CHECK_WORD_IS_NULL,this.checkWord);

        this.loginError = checkLoginError();
    }
}
